package com.tuotu.gps.adapter;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageSwitcher;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.clam314.lame.MediaPlayUtil;
import com.tuotu.gps.R;
import com.tuotu.gps.entity.LocationInfo;
import com.tuotu.gps.utils.LocationString;
import com.tuotu.gps.utils.LogUtil;
import com.tuotu.gps.utils.Utils;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * Created by XUYEWEN on 2017/7/23.
 */

public class MyRecycleViewAdapter extends RecyclerView.Adapter {

    private List<LocationInfo> list;
    private Context mContext;
    private onClickListener onClickListener = null;
    private SharedPreferences preferences;
    private String GPS_PATH;
    private int selectedPosition = 0;

    public MyRecycleViewAdapter(Context context, List<LocationInfo> list, SharedPreferences preferences) {
        mContext = context;
        this.list = list;
        this.preferences = preferences;
    }

    public void setSelectedPosition(int selectedPosition) {
        this.selectedPosition = selectedPosition;
    }

    public int getSelectedPosition() {
        return selectedPosition;
    }

    public List<LocationInfo> getList() {
        return list;
    }

    public void setList(List<LocationInfo> list, String GPS_PATH) {
        this.list = list;
        this.GPS_PATH = GPS_PATH;
    }

    public interface onClickListener {
        void onItemClick(View view, int position);

        void onItemLongClick(View view, int position);
    }

    public void setOnClickListener(onClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext
        ).inflate(R.layout.list_item, parent,
                false);
        ViewHolder1 holder = new ViewHolder1(view);
        return holder;
    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ((ViewHolder1) holder).itemView.setSelected(selectedPosition == position);
        ((ViewHolder1) holder).ibPlay.setChecked(selectedPosition == position?MediaPlayUtil.isPlaying():false);
        ((ViewHolder1) holder).tvNumber.setText(" "+(position+1));
//        String name = list.get(position).getMp3Name();
//        String str = name.substring(11, name.length() - 4).replace("_", ":");
//        ((ViewHolder1) holder).tvNameMp3.setText(str);
        String gpsinfo = Utils.readFile(list.get(position).getTxtPathName());
        if (null != gpsinfo) {
//            Log.i("xwww","gpsinfo =="+gpsinfo);
            int duration = 0;
            try {
                JSONObject jsonObject = new JSONObject(gpsinfo);
//                double lon=jsonObject.getDouble("lon");
//                double lat=jsonObject.getDouble("lat");
                double lon=Double.parseDouble(Utils.getFromBase64(jsonObject.getString("lon")));
                double lat=Double.parseDouble(Utils.getFromBase64(jsonObject.getString("lat")));
                ((ViewHolder1)holder).tvGpsInfo.setText((int)(lon*100000)/(float)100000.0+" "+(int)(lat*100000)/(float)100000.0); //显示gps经纬度
                double direction=Double.parseDouble(Utils.getFromBase64(jsonObject.getString("direction")));
                ((ViewHolder1) holder).tvDirection.setText(Utils.getDirection(direction));
//                long t1=preferences.getLong(list.get(position).getMp3Name(), 0);
//                long t1 = jsonObject.getLong("collect_time");
                long t1=Long.parseLong(Utils.getFromBase64(jsonObject.getString("collect_time")));
                if (0 != t1) {
                    Date pDate=new Date(t1);
                    String pMp3String=Utils.itemTimeFormat.format(pDate);
                    ((ViewHolder1) holder).tvNameMp3.setText(pMp3String);
                    ((ViewHolder1) holder).tvNameTxt.setVisibility(View.VISIBLE);
                    Date date=new Date();
                    long t2 = date.getTime() - t1;
                    int day = (int) (t2 / 24 / 60 / 60 / 1000);
                    int hour = (int) (t2 / 60 / 60 / 1000);
                    int fen = (int) (t2 / 60 / 1000);
                    int miao = (int) (t2 / 1000);
                    if (day > 0) {
                        ((ViewHolder1) holder).tvNameTxt.setText(day + "天");
                    } else if (hour > 0) {
                        ((ViewHolder1) holder).tvNameTxt.setText(hour + "小时");
                    } else if (fen > 0) {
                        ((ViewHolder1) holder).tvNameTxt.setText(fen + "分钟前");
                    } else {
                        ((ViewHolder1) holder).tvNameTxt.setText(miao + "秒钟前");
                    }
                } else {
                    ((ViewHolder1) holder).tvNameTxt.setVisibility(View.GONE);
                }

//                duration = jsonObject.getInt("mp3_duration");
                duration=Integer.parseInt(Utils.getFromBase64(jsonObject.getString("mp3_duration")));
//                LogUtil.printE("duration=="+duration);
            } catch (JSONException e) {
                e.printStackTrace();
            } catch (Exception e) {

            }

            ((ViewHolder1) holder).tvDuration0.setVisibility(duration == 0 ? View.INVISIBLE : View.VISIBLE);
            ((ViewHolder1) holder).tvDuration.setVisibility(duration == 0 ? View.INVISIBLE : View.VISIBLE);
            if (duration != 0){
                ((ViewHolder1) holder).tvDuration.setText(duration + "秒");
                if(duration>4){
                    ((ViewHolder1) holder).tvDuration0.setTextColor(Color.WHITE);
                    ((ViewHolder1) holder).tvDuration.setTextColor(Color.WHITE);
                }else {
                    ((ViewHolder1) holder).tvDuration0.setTextColor(ContextCompat.getColor(mContext,R.color.colorOrange));
                    ((ViewHolder1) holder).tvDuration.setTextColor(ContextCompat.getColor(mContext,R.color.colorOrange));
                }
            }
//            try{
//                int index0 = gpsinfo.lastIndexOf(":");//"."最后出现的位置
//                ((ViewHolder1)holder).tvDirection.setText(getDirection(gpsinfo.substring(index0+1,gpsinfo.length())));
//            }catch (Exception e){
//
//            }
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }


    class ViewHolder1 extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {

        ToggleButton ibPlay;
        ImageButton ibDelete;
        TextView tvNameMp3;
        TextView tvNameTxt;
        TextView tvDirection;
        TextView tvGpsInfo;
        TextView tvDuration;
        TextView tvDuration0;
        TextView tvNumber;


        public ViewHolder1(View itemView) {
            super(itemView);
            ibPlay = (ToggleButton) itemView.findViewById(R.id.ib_play);
            tvNumber = (TextView) itemView.findViewById(R.id.tv_number);
            tvNameMp3 = (TextView) itemView.findViewById(R.id.tv_name_mp3);
            tvNameTxt = (TextView) itemView.findViewById(R.id.tv_name_txt);
            tvDirection = (TextView) itemView.findViewById(R.id.tv_direction);
            tvGpsInfo= (TextView) itemView.findViewById(R.id.tv_name_gpsinfo);
            tvDuration = (TextView) itemView.findViewById(R.id.tv_duration);
            tvDuration0 = (TextView) itemView.findViewById(R.id.tv_duration0);

            itemView.setOnClickListener(this);
            itemView.setOnLongClickListener(this);
            ibPlay.setOnClickListener(this);
            ibPlay.setTag(itemView.getId());
//            ibPlay.setOnClickListener(this);
//            ibDelete.setOnClickListener(this);
//            tvNameMp3.setOnClickListener(this);
//            tvNameTxt.setOnClickListener(this);
//            tvDirection.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            LogUtil.print(" onClick getAdapterPosition==" + getAdapterPosition());
            ibPlay.setChecked(false);
            onClickListener.onItemClick(view, getAdapterPosition());
        }

        @Override
        public boolean onLongClick(View view) {
            onClickListener.onItemLongClick(view, getAdapterPosition());
            return true;
        }
    }

}
